All files / src/hooks useCategories.ts

0% Statements 0/20
0% Branches 0/4
0% Functions 0/4
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                                 
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { apiService } from '@/services/api';
import { apiErrorMessage } from '@/lib/utils';
 
export interface Category {
  id: number;
  name: string;
  category_type: string;
}
 
export const useCategories = () => {
  const { t } = useTranslation();
  return useQuery({
    queryKey: ['categories'],
    queryFn: async (): Promise<Category[]> => {
      const result = await apiService.get<Category[]>('/api/reseller/categories');
      if (result.success) {
        return result.data;
      }
      throw new Error(apiErrorMessage(result.error, t('products.errors.loadCategoriesFailed')));
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};
 
export const useCategoriesAdmin = () => {
  const { t } = useTranslation();
  return useQuery({
    queryKey: ['categories-admin'],
    queryFn: async (): Promise<Category[]> => {
      const result = await apiService.get<Category[]>('/api/admin/categories');
      if (result.success) {
        return result.data;
      }
      throw new Error(apiErrorMessage(result.error, t('products.errors.loadCategoriesFailed')));
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};